package org.luxor.eshop.storage.controller;

import lombok.extern.slf4j.Slf4j;
import org.luxor.commons.core.web.controller.BaseRestController;
import org.luxor.commons.core.web.data.R;
import org.luxor.eshop.storage.entity.dto.CommodityDto;
import org.luxor.eshop.storage.entity.dto.DeductInventoryDto;
import org.luxor.eshop.storage.service.StorageService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collection;


/**
 * 仓储服务
 *
 * @author Mr.Yan  @date 2019/9/19
 */
@Slf4j
@RestController
@RequestMapping("/storage/commodity")
public class StorageController extends BaseRestController {

    @Resource
    private StorageService storageService;

    /**
     * 对给定的商品扣除仓储数量。
     */
    @PreAuthorize("isAnonymous()")
    @PostMapping("/deductInventory")
    public R deductInventory(@RequestBody Collection<DeductInventoryDto> deductInventory) {
        storageService.deduct(deductInventory);
        return R.ok();
    }

    /**
     * 获取商品信息
     */
    @PreAuthorize("isAnonymous()")
    @GetMapping("/{commodityCode}")
    public R<CommodityDto> get(@PathVariable String commodityCode) {
        CommodityDto accountInfo = storageService.getByCommodityCode(commodityCode);
        return R.ok(accountInfo);
    }
}
